package net.vkits.controller;

import net.vkits.anjiamiao.application.PayRecordApplicationService;
import net.vkits.anjiamiao.domain.model.PayRecord;
import net.vkits.common.alipay.AlipayDTO;
import org.dozer.DozerBeanMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.annotation.Resource;

/**
 * Created by ethan-liu on 2016/12/4.
 */
@Controller
@RequestMapping("/payment")
public class PaymentController {

    private static final Logger logger = LoggerFactory.getLogger(PaymentController.class);
    @Resource
    private DozerBeanMapper mapper;
    @Resource
    private PayRecordApplicationService applicationService;

    @RequestMapping(value = "/notify",
            produces = { "text/html;charset=UTF-8" },
            method = RequestMethod.POST)
    ResponseEntity<String> notify(AlipayDTO dto){
        logger.info("get alipay params: {}", dto);
        PayRecord record = mapper.map(dto, PayRecord.class);
        applicationService.addPayRecord(record);
        return new ResponseEntity<>("SUCCESS", HttpStatus.OK);
    }
}
